import 'package:flutterhello/pages/chat_detail/chat_detail.dart';

class SortUtil {
  static Map chatMapSort(chatMap) {
    List<Map<String, List<dynamic>>> listData = [];
    chatMap.forEach((key, value) => listData.add({key: value}));
    listData.sort((a, b) {
      String aTime = a.values.first.last.time;
      String bTime = b.values.first.last.time;
      return DateTime.parse(aTime).isBefore(DateTime.parse(bTime)) ? 0 : 1;
    });
    print(listData.toString());
    Map<String, List<dynamic>> sortedMap = Map.fromIterable(listData,
        key: (e) => e.keys.first, value: (e) => e.values.first);
    print(chatMap);
    print(sortedMap);
    return sortedMap;
  }

  static List groupSort(List group, Map chatMap) {
    List newGroup = group;
    chatMap.keys.forEach((element) {
      int length = group.length;
      for (int i = 0; i < length; i++) {
        var item = group[i];
        if (item["id"] == element) {
          newGroup.remove(item);
          newGroup.add(item);
        }
      }
    });
    return newGroup;
  }
}
